package com.ruoyi.xp.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.xp.order.service.IOrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xp.order.mapper.OrderItemHeadPackageMapper;
import com.ruoyi.xp.order.domain.OrderItemHeadPackage;
import com.ruoyi.xp.order.service.IOrderItemHeadPackageService;
import com.ruoyi.common.core.text.Convert;

/**
 * 虾皮订单明细外部采购货物信息Service业务层处理
 * 
 * @author chdx
 * @date 2023-01-22
 */
@Service
public class OrderItemHeadPackageServiceImpl implements IOrderItemHeadPackageService 
{
    @Autowired
    private OrderItemHeadPackageMapper orderItemHeadPackageMapper;

    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 查询虾皮订单明细外部采购货物信息
     * 
     * @param orderItemHeadPackageId 虾皮订单明细外部采购货物信息主键
     * @return 虾皮订单明细外部采购货物信息
     */
    @Override
    public OrderItemHeadPackage selectOrderItemHeadPackageByOrderItemHeadPackageId(Long orderItemHeadPackageId)
    {
        return orderItemHeadPackageMapper.selectOrderItemHeadPackageByOrderItemHeadPackageId(orderItemHeadPackageId);
    }

    /**
     * 查询虾皮订单明细外部采购货物信息列表
     * 
     * @param orderItemHeadPackage 虾皮订单明细外部采购货物信息
     * @return 虾皮订单明细外部采购货物信息
     */
    @Override
    public List<OrderItemHeadPackage> selectOrderItemHeadPackageList(OrderItemHeadPackage orderItemHeadPackage)
    {
        return orderItemHeadPackageMapper.selectOrderItemHeadPackageList(orderItemHeadPackage);
    }

    /**
     * 新增虾皮订单明细外部采购货物信息
     * 
     * @param orderItemHeadPackage 虾皮订单明细外部采购货物信息
     * @return 结果
     */
    @Override
    public int insertOrderItemHeadPackage(OrderItemHeadPackage orderItemHeadPackage)
    {
        return orderItemHeadPackageMapper.insertOrderItemHeadPackage(orderItemHeadPackage);
    }

    /**
     * 修改虾皮订单明细外部采购货物信息
     * 
     * @param orderItemHeadPackage 虾皮订单明细外部采购货物信息
     * @return 结果
     */
    @Override
    public int updateOrderItemHeadPackage(OrderItemHeadPackage orderItemHeadPackage)
    {
        return orderItemHeadPackageMapper.updateOrderItemHeadPackage(orderItemHeadPackage);
    }

    /**
     * 批量删除虾皮订单明细外部采购货物信息
     * 
     * @param orderItemHeadPackageIds 需要删除的虾皮订单明细外部采购货物信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemHeadPackageByOrderItemHeadPackageIds(String orderItemHeadPackageIds)
    {
        return orderItemHeadPackageMapper.deleteOrderItemHeadPackageByOrderItemHeadPackageIds(Convert.toStrArray(orderItemHeadPackageIds));
    }

    /**
     * 删除虾皮订单明细外部采购货物信息信息
     * 
     * @param orderItemHeadPackageId 虾皮订单明细外部采购货物信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderItemHeadPackageByOrderItemHeadPackageId(Long orderItemHeadPackageId)
    {
        return orderItemHeadPackageMapper.deleteOrderItemHeadPackageByOrderItemHeadPackageId(orderItemHeadPackageId);
    }

    /**
     * 根据订单号删除订单明细外部采购信息
     *
     * @param orderSn
     */
    @Override
    public void deleteOrderItemHeadPackageByOrderSn(String orderSn){
        OrderItemHeadPackage orderItemHeadPackage = new OrderItemHeadPackage();
        orderItemHeadPackage.setOrderSn(orderSn);
        List<OrderItemHeadPackage> orderItemHeadPackageList = orderItemHeadPackageMapper.selectOrderItemHeadPackageList(orderItemHeadPackage);
        String[] orderItemHeadPackageIds = new String[orderItemHeadPackageList.size()];
        for (int i = 0; i < orderItemHeadPackageList.size(); i++) {
            orderItemHeadPackageIds[i] = String.valueOf(orderItemHeadPackageList.get(i).getOrderItemHeadPackageId());
        }
        orderItemHeadPackageMapper.deleteOrderItemHeadPackageByOrderItemHeadPackageIds(orderItemHeadPackageIds);
    }

    /**
     * 获取没有采购价的订单明细
     */
    @Override
    public boolean updatePurchasePrice(String orderSn, String purchaseOrderSn, BigDecimal purchasePrice) {
        OrderItemHeadPackage orderItemHeadPackageQuery = new OrderItemHeadPackage();
        orderItemHeadPackageQuery.setOrderSn(orderSn);
        orderItemHeadPackageQuery.setPurchaseOrderSn(purchaseOrderSn);
        List<OrderItemHeadPackage> orderItemHeadPackageList = orderItemHeadPackageMapper.selectOrderItemHeadPackageList(orderItemHeadPackageQuery);
        for (OrderItemHeadPackage orderItemHeadPackage : orderItemHeadPackageList) {
            orderItemHeadPackage.setPurchasePrice(purchasePrice);
            orderItemHeadPackage.setPurchaseTotalPrice(purchasePrice.multiply(new BigDecimal(orderItemHeadPackage.getQuantity())));
            orderItemHeadPackageMapper.updateOrderItemHeadPackage(orderItemHeadPackage);
        }
        orderItemService.updatePurchasePrice(orderSn);
        return true;
    }
}
